package com.celesea.auth.user.vo;

import com.celesea.auth.user.entity.User;
import com.celesea.auth.user.entity.UserPerm;
import com.celesea.auth.user.v1.controller.request.PostRequest;
import com.celesea.framework.util.CollectionKit;
import com.celesea.framework.util.Const;
import com.celesea.framework.util.EnvUtil;
import com.celesea.framework.util.PinyinKit;
import com.celesea.framework.vo.DictionaryVo;
import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;

import java.util.List;

/**
 * @author JackYuan
 * @create 2017-10-29 22:25
 */
public class UserVo extends User {

    private DictionaryVo dept;
    private DictionaryVo grade;
    private DictionaryVo leader;
    private List<DictionaryVo> permUsers;
    private List<DictionaryVo> permDepts;

    public UserVo() {
    }

    public UserVo(PostRequest request) {
        BeanUtils.copyProperties(request, this);
        this.setPy(PinyinKit.getPinyinFirst(this.getName()));
        this.setPassword(this.getAccount());
        if (request.getDept() != null) {
            this.setDeptId(request.getDept().getId());
            this.setDeptName(request.getDept().getName());
        }
        if (request.getGrade() != null) {
            this.setGradeId(request.getGrade().getId());
            this.setGradeName(request.getGrade().getName());
        }
        if (request.getLeader() != null) {
            this.setLeaderId(request.getLeader().getId());
            this.setLeaderName(request.getLeader().getName());
        }
        boolean permFlag = CollectionKit.isNotEmpty(permUsers) || CollectionKit.isNotEmpty(permDepts);
        this.setPermFlag(Const.Bool.valueOf(permFlag));
    }

    public User toEntity() {
        User entity = new User();
        BeanUtils.copyProperties(this, entity);
        return entity;
    }

    public User toUnique() {
        User entity = new User();
        entity.setId(this.id);
        entity.setAccount(this.getAccount());
        entity.setTenantId(EnvUtil.getTenantId());
        return entity;
    }

    public void setUserPerms(List<UserPerm> userPerms) {
        if (CollectionKit.isNotEmpty(userPerms)) {
            this.permUsers = Lists.newArrayList();
            this.permDepts = Lists.newArrayList();
            userPerms.forEach(item -> {
                DictionaryVo dic = new DictionaryVo(item.getPermId(), item.getPermName());
                if (UserPerm.PERM_TYPE_USER == item.getPermType()) {
                    this.permUsers.add(dic);
                } else {
                    this.permDepts.add(dic);
                }
            });
        }
    }

    public List<UserPerm> getUserPerms() {
        List<UserPerm> userPerms = Lists.newArrayList();
        if (CollectionKit.isNotEmpty(permUsers)) {
            permUsers.forEach(item -> {
                userPerms.add(new UserPerm(this, UserPerm.PERM_TYPE_USER, item));
            });
        }
        if (CollectionKit.isNotEmpty(permDepts)) {
            permDepts.forEach(item -> {
                userPerms.add(new UserPerm(this, UserPerm.PERM_TYPE_DEPT, item));
            });
        }
        return userPerms;
    }

    public UserVo(User user) {
        BeanUtils.copyProperties(user, this);
    }

    public DictionaryVo getDept() {
        return dept;
    }

    public void setDept(DictionaryVo dept) {
        this.dept = dept;
    }

    public DictionaryVo getGrade() {
        return grade;
    }

    public void setGrade(DictionaryVo grade) {
        this.grade = grade;
    }

    public DictionaryVo getLeader() {
        return leader;
    }

    public void setLeader(DictionaryVo leader) {
        this.leader = leader;
    }

    public List<DictionaryVo> getPermUsers() {
        return permUsers;
    }

    public void setPermUsers(List<DictionaryVo> permUsers) {
        this.permUsers = permUsers;
    }

    public List<DictionaryVo> getPermDepts() {
        return permDepts;
    }

    public void setPermDepts(List<DictionaryVo> permDepts) {
        this.permDepts = permDepts;
    }


}
